	package com.pengtu.web.project.labs;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Organization;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 单位管理
 * 
 * @author libitum
 * 
 */
@Results({
		@Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "organization.action"),
		@Result(name = BaseActionSupport.LOGIN, type = "redirect", location = "/project/labs/design.action?projectId=${projectId}&contractKind=${typeId}&deptId=${deptId}")
})
public class OrganizationAction extends BaseActionSupport<Organization> {

	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	// -- 页面属性 --//
	private String id;
	private Organization entity;
	private List<Organization> allOrganizationList;
	private Page<Organization> page = new Page<Organization>(10);// 每页5条记录
	private String typeId;
	private String projectId;
	private String deptId;
	private String signid;

	public String getSignid() {
		return signid;
	}

	public void setSignid(String signid) {
		this.signid = signid;
	}

	public String list() throws Exception {
		QueryFilter filters = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (filters == null) {
			allOrganizationList = accountManager.getOrganization();
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchOrganization(page, filters);
		return SUCCESS;
	}
	@Override
	public String input() throws Exception {
	System.out.println("orgid=="+entity.getId());
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		accountManager.saveOrganization(entity);
		addActionMessage("保存单位成功");
//		if (StringUtils.isNotEmpty(projectId))
//			return LOGIN;
		return input();
	}


	/**
	 * 验证机构代码是否重复
	 */
	public void checkOrgNum() {
		// TODO Auto-generated method stub
		String orgNum = Struts2Utils.getParameter("orgNum");
		String oldOrgNum = Struts2Utils.getParameter("oldOrgNum");
		if (accountManager.isOrgNumUnique(orgNum, oldOrgNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	/**
	 * 验证单位名是否重复
	 */
	public void checkOrgName() {
		// TODO Auto-generated method stub
		String orgName = Struts2Utils.getParameter("orgName");
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		if (accountManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		accountManager.deleteOrganization(id);
		addActionMessage("删除单位信息");
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getOrganization(id);
		} else {
			entity = new Organization();
		}
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Organization getEntity() {
		return entity;
	}

	public void setEntity(Organization entity) {
		this.entity = entity;
	}

	public List<Organization> getAllOrganizationList() {
		return allOrganizationList;
	}

	public Page<Organization> getPage() {
		return page;
	}

	public void setPage(Page<Organization> page) {
		this.page = page;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	@Override
	public Organization getModel() {
		return entity;
	}

}
